<?php
/*
 * Joomla! 1.5 component ArticleCashRewards
 *
 * @version $Id: rejected.php 2009-05-10 01:47:01 svn $
 * @author Maverick
 * @package controllers
 * @subpackage ArticleCashRewards
 * @license GNU/GPL
 * Website: http://www.boddunan.com
 *
 * Review and approve the articles and assign points and cash rewards.
 * Users can see their articles and the points/cash rewards assigned in the front-end module.
 */

defined('_JEXEC') or die();

class ArticleCashRewardsControllerWebLinks extends ArticleCashRewardsController
{

	function __construct(){
		parent::__construct();
        global $mainframe, $acr_config;
        $acr_config = $mainframe->getUserState( SESSION_ACR_CONFIG );

        if(!isset($acr_config)){
            $model = & $this->getModel('config');
            $configt = $model->getConfiguration();

            foreach($configt as $ct){
                $acr_config[$ct->config_name] = $ct->config_value;
            }
            $mainframe->setUserState( SESSION_ACR_CONFIG, $acr_config );
        }
        $this->registerTask( 'review','reviewWebLink' );
        $this->registerTask( 'pending','pendingWebLinks' );
        $this->registerTask( 'approved','approvedWebLinks' );
        $this->registerTask( 'rejected','rejectedWebLinks' );
        $this->registerTask( 'approve','approveWebLinks' );
        $this->registerTask( 'disapprove','disApproveWebLinks' );
        $this->registerTask( 'cPanel','cPanel' );
	}

	function reviewWebLink(){
		$view = & $this->getView('weblinks', 'html');
		$model = & $this->getModel('weblinks');
		$view->setModel($model, true);
        $view->setLayout('review');
        $view->display();
	}

	function pendingWebLinks(){
		$view = & $this->getView('weblinks', 'html');
		$model = & $this->getModel('weblinks');
		$view->setModel($model, true);
        $view->setLayout('pending');
        $view->display();
	}

	function approvedWebLinks(){
		$view = & $this->getView('weblinks', 'html');
		$model = & $this->getModel('weblinks');
		$view->setModel($model, true);
        $view->setLayout('approved');
        $view->display();
	}

	function rejectedWebLinks(){
		$view = & $this->getView('weblinks', 'html');
		$model = & $this->getModel('weblinks');
		$view->setModel($model, true);
        $view->setLayout('rejected');
        $view->display();
	}

	function getWebLink(){
		$view = & $this->getView('weblinks', 'html');
		$model = & $this->getModel('weblinks');
		$view->setModel($model, true);
        $view->setLayout('review');
        $view->display();
	}

    function approveWebLinks(){
        global $mainframe;
        $cids = JRequest::getVar( 'cid', null, '', Array() );
        $redirect = JRequest::getVar( 'redirect', STATUS_PENDING, 'post', 'string' );
        $redirect = ($redirect == STATUS_PENDING)?'pending':
            (($redirect == STATUS_APPROVED)?'approved':'rejected');
		if (count($cids) < 1) {
			$msg =  JText::_('Select an item to approve');
			$mainframe->redirect('index.php?option=com_articlecashrewards&controller=weblinks&task=pending', $msg);
		}
        
		$model = & $this->getModel('weblinks');
        if($model->setApprovalStatus($cids, STATUS_APPROVED)){
            $model = & $this->getModel('reports');
            $uids = $model->getUserIds($cid, 1);
            if($model->recalculateEarnings($uids)){
                $message = JText::_('INFO_WEBLINK_APPROVED');
            }else{
                $message = JText::_('INFO_WEBLINK_FAILED') . ": at phase 2";
                $message .= ' ['.$model->getError().'] ';
            }
		} else {
			$message = JText::_('INFO_WEBLINK_FAILED');
			$message .= ' ['.$model->getError().'] ';
		}
        $this->setRedirect('index.php?option=com_articlecashrewards&controller=weblinks&task='.$redirect, $message);
    }
    
    function disApproveWebLinks(){
        global $mainframe;
        $cids = JRequest::getVar( 'cid', null );
        $redirect = JRequest::getVar( 'redirect', STATUS_PENDING, 'post', 'string' );
        $redirect = ($redirect == STATUS_PENDING)? 'pending':
            (($redirect == STATUS_APPROVED)?'approved':'rejected');
		if (count($cids) < 1) {
			$msg =  JText::_('Select an item to disapprove');
			$mainframe->redirect('index.php?option=com_articlecashrewards&controller=weblinks&task=pending', $msg);
		}

		$model = & $this->getModel('weblinks');
        if($model->setApprovalStatus($cids, STATUS_REJECTED)){
            $model = & $this->getModel('reports');
            $uids = $model->getUserIds($cid, 1);
            if($model->recalculateEarnings($uids)){
            	$message = JText::_('INFO_WEBLINK_DISAPPROVED');
            } else {
                $message = JText::_('INFO_WEBLINK_FAILED');
                $message .= ' ['.$model->getError().'] ';
            }
        } else {
                $message = JText::_('INFO_WEBLINK_FAILED');
                $message .= ' ['.$model->getError().'] ';
        }
        $this->setRedirect('index.php?option=com_articlecashrewards&controller=weblinks&task='.$redirect, $message);
    }

    function cPanel(){
        $link = 'index.php?option=com_articlecashrewards&controller=cpanel';
        $this->setRedirect($link);
    }
}
?>
